package com.david.trade.api.controller;

import com.david.trade.api.event.TcpEventListener;
import com.david.trade.api.event.TcpEventSource;
import com.david.trade.api.tcp.NettyTcpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 订单相关接口
 * @author admin
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private NettyTcpClient nettyTcpClient;

    Map<String, TcpEventSource> map = new HashMap<>();

    @GetMapping("create")
    public Flux<TcpEventSource.TcpEvent> create() {

        TcpEventSource eventSource = new TcpEventSource();
        nettyTcpClient.sendMessage(UUID.randomUUID().toString(), eventSource, "aabbccdd");
        return Flux.create(sink -> {
                    eventSource.register(new TcpEventListener() {
                        @Override
                        public void onNewEvent(TcpEventSource.TcpEvent event) {
                            sink.next(event);
                        }

                        @Override
                        public void onEventStopped() {
                            sink.complete();
                        }
                    });
                }
        );
    }


}
